package com.power.sifa.respond.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.plf.common.domain.PageDataResponse;
import com.plf.common.domain.ResponseDTO;
import com.plf.common.utils.ExcelUtil;
import com.power.sifa.respond.api.IApl_respondFeign;
import com.power.sifa.respond.model.Apl_respond;
import com.power.sifa.respond.model.pageInfo.Apl_respondPageInfo;
import com.power.sifa.respond.service.Apl_respondService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;


/**
 *   行政应诉申请控制器
 */

@Slf4j
//@PowerLog
@RestController
@Api(value = "Apl_respondController",tags={"行政应诉申请"})
public class Apl_respondController implements IApl_respondFeign {

    @Resource
    private Apl_respondService apl_respondService;


    @Override
    @ApiOperation("查询行政应诉申请分页列表")
    @SaCheckPermission("apl_respond_list")
    public ResponseDTO list(Apl_respondPageInfo pageInfo) {
        Assert.notNull(pageInfo, "pageInfo参数不能为空");
        Page<Apl_respond> page = this.apl_respondService.findAll(pageInfo);
        List list = new ArrayList();
        if (!page.isEmpty()) {
            for (Apl_respond apl_respond : page) {
                apl_respond.geneText();
                list.add(apl_respond);
            }
        }
        PageDataResponse p = new PageDataResponse(list, page.getTotalElements(), page.getSize(), pageInfo.getPageNumber()<=1 ? 1 : pageInfo.getPageNumber());
        return ResponseDTO.succData(p);
    }

    @Override
    @ApiOperation("查看行政应诉申请")
    @SaCheckPermission("apl_respond_view")
    public ResponseDTO view(String id) {
        Assert.notNull(id, "id不能为空");
        Apl_respond apl_respond  = apl_respondService.getById(id);
        if (apl_respond != null) {
            apl_respond.geneText();
        }
        return ResponseDTO.succData(apl_respond);
    }

    @Override
    @ApiOperation("删除行政应诉申请")
    @SaCheckPermission("apl_respond_del")
    public ResponseDTO delete(String id) {
        Assert.notNull(id, "id不能为空");
        apl_respondService.removeById(id);
        return ResponseDTO.succ();
    }

    @Override
    @ApiOperation("保存或修改行政应诉申请")
    @SaCheckPermission("apl_respond_save")
    public ResponseDTO save(Apl_respond apl_respond) {
        Assert.notNull(apl_respond, "apl_respond不能为空");
        apl_respondService.saveOrUpdate(apl_respond);
        return ResponseDTO.succ();
    }

    @Override
    @ApiOperation(value="批量删除行政应诉申请")
    @SaCheckPermission("apl_respond_del")
    public ResponseDTO batchDel(String ids) {
        Assert.isTrue(StringUtils.hasText(ids), "ids不能为空");
        List<Integer> list = Arrays.asList(ids.split(",")).stream().map(s -> Integer.parseInt(s.trim())).collect(Collectors.toList());
        apl_respondService.removeByIds(list);
        return ResponseDTO.succ();
    }

    @Override
    @ApiOperation(value="导出excel")
    @SaCheckPermission("apl_respond_list")
    public void output(HttpServletResponse response, Apl_respondPageInfo pageInfo) {
        Assert.notNull(pageInfo, "pageInfo不能为空");
        String title = "行政应诉申请";
        String[] heads = {"序号applyid","案件类型","审结状态","一审原审案件编号","是否经过复议","复议案件编号","复议申请人","复议被申请人","复议事项","关联信息行政管理类别","复议请求","复议决定","收到应诉通知书申请日期","诉讼请求","起诉事项","应诉信息行政管理类别","是否收到司法建议","是否落实司法建议","是否附带对规范性文件审查","原告","是否存在第三人","机关应诉类型","立案日期","结案日期","文件名称","制定机关","审查机关","审查结果"};
        String[] fields = {"id","casetype","casestatus","firstcode","isrecon","reconcode","applyname","targetname","prtype","asmgrcate","prrequest","decisiontype","noticedate","requestre","suetype","remgrcate","isreceive","isfulfill","isdocreview","plaintiffname","isthird","organtype","regisdate","closedate","filename","fororgan","exaorgan","exaresult"};
        Page<Apl_respond> page = this.apl_respondService.findAll(pageInfo);
        ExcelUtil.exportXls(title, heads, fields, page.getContent(), response);
    }

    @Override
    @ApiOperation(value="导出excel模板")
    public void exportExcelTemplate(HttpServletResponse response) {
        String title = "行政应诉申请";
        //id自增或list从表可自行删除，需与导入项一致
        String[] heads = {"序号applyid","案件类型","审结状态","一审原审案件编号","是否经过复议","复议案件编号","复议申请人","复议被申请人","复议事项","关联信息行政管理类别","复议请求","复议决定","收到应诉通知书申请日期","诉讼请求","起诉事项","应诉信息行政管理类别","是否收到司法建议","是否落实司法建议","是否附带对规范性文件审查","原告","是否存在第三人","机关应诉类型","立案日期","结案日期","文件名称","制定机关","审查机关","审查结果"};
        ExcelUtil.exportXls(title, heads, null, null, response);
    }

    @Override
    public ResponseDTO findList(Apl_respondPageInfo pageInfo) {
        try {
            return ResponseDTO.succData(this.apl_respondService.findList(pageInfo));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("/apl_respond/findList 行政应诉关联查询主表失败 ->  msg[{}]", e.getMessage());
            return ResponseDTO.wrap(500, "行政应诉关联查询主表失败");
        }
    }

    @Override
	@ApiOperation(value="导入excel")
	public ResponseDTO importExcel(MultipartFile file) throws IOException {
        Assert.notNull(file, "file不能为空");
        //id自增或list从表可自行删除，需与下载的模板中导入项一致
        String[] fields = {"id","casetype","casestatus","firstcode","isrecon","reconcode","applyname","targetname","prtype","asmgrcate","prrequest","decisiontype","noticedate","requestre","suetype","remgrcate","isreceive","isfulfill","isdocreview","plaintiffname","isthird","organtype","regisdate","closedate","filename","fororgan","exaorgan","exaresult"};
        List<Apl_respond> list = (List<Apl_respond>)ExcelUtil.Import(fields, file, Apl_respond.class);
        if(!CollectionUtils.isEmpty(list)) {
            this.apl_respondService.saveAll(list);
        }
        return ResponseDTO.succ();
	}

}
